home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
JCSM Shareware Collection 1996 September
/
JCSM Shareware Collection (JCS Distribution) (September 1996).ISO
/
uother__
/
sndx10.zip
/
SOUNDEX.C
< prev
next >
Wrap
C/C++ Source or Header
|
1994-11-06
|
3KB
|
130 lines
/**************************************************************************/
/* Soundex Code Generator */
/* Generates the soundex equivalent of input name */
/* */
/* M\Cooper */
/* 3425 Chestnut Ridge Rd. */
/* Grantsville, MD 21536 */
/* email: thegrendel@aol.com */
/* */
/* Source code placed in the Public Domain 10/92 */
/**************************************************************************/
#include <stdio.h>
#include <string.h>
#include "soundex.h"
boolean prune_str( char* str, char pr_char )
//Deletes Char pr_char from String str & returns TRUE (1) if successful
{
char* ptr;
if( !( ptr = strchr( str, pr_char ) ) )
return (FALSE);
*ptr = NULL;
strcat( str, ++ptr );
return (TRUE);
}
char xletter( char character )
//Translates Char into numerical code
{
char code = '*';
switch ( character )
{
case 'B':
case 'F':
case 'P':
case 'V': code = '1';
break;
case 'C':
case 'G':
case 'J':
case 'K':
case 'Q':
case 'S':
case 'X':
case 'Z': code = '2';
break;
case 'D':
case 'T': code = '3';
break;
case 'L': code = '4';
break;
case 'M':
case 'N': code = '5';
break;
case 'R': code = '6';
break;
default : code = '*'; // Marker to delete index, later
}
return (code );
}
char* soundex( char* name )
{
char *workstr,
*strhead,
prevchar;
static char zerostr[] = "0000";
size_t length;
workstr = strdup ( name );
workstr = strupr ( workstr );
strhead = workstr; // Keep track of string head.
++workstr; // Preserve first letter.
while ( *workstr )
{
prevchar = *workstr; // Keep track of previous character.
*workstr++ = xletter( *workstr );
if( *workstr == prevchar ) // Replace duplicate char
*workstr = '*'; // with asterisk [later to be deleted].
}
while( prune_str( strhead, '*' ) );
if( ( length = SXLENGTH - strlen( strhead ) ) > 0 )
strncat( strhead, zerostr, length ); //If length < 4, fill with 0's
*( strhead + SXLENGTH ) = NULL; // Truncate to max. of 4 chars.
return( strhead );
}
void main( int argc, char* argv[] )
{
char name [MAXLEN];
if( argc == 2 )
{
printf("\nThe soundex conversion of %s is: %s",
argv[1], soundex( argv[1] ) );
}
else
{
puts( "\n\nEnter a name, please... " );
gets( name );
printf("\nThe soundex conversion of %s is: %s",
name, soundex( name ) );
}
}